﻿using System;
using System.Collections;
using System.Windows.Forms;

namespace SimpleMediaManager.Controls
{
    public class ListViewColumnSorter : IComparer
    {
        private CaseInsensitiveComparer comparer = new CaseInsensitiveComparer();

        public int Column { get; set; }
        public SortOrder Sorting { get; set; }

        public int Compare(object x, object y)
        {
            ListViewItem listviewX = (ListViewItem)x;
            ListViewItem listviewY = (ListViewItem)y;

            int compareResult = comparer.Compare(listviewX.SubItems[Column].Text, listviewY.SubItems[Column].Text);

            if (Sorting == SortOrder.Ascending)
            {
                return compareResult;
            }
            else if (Sorting == SortOrder.Descending)
            {
                return (-compareResult);
            }
            else
            {
                return 0;
            }
        }
    }
}
